package BinaryTree;
/**
 * 平衡二叉树
 */
import BinaryTree.init.TreeNode;

public class lc110 {
    public boolean isBalanced(TreeNode root) {
        return getHeight(root) != -1;
    }
    public int getHeight(TreeNode root) {
        if (root == null) return 0;
        int lefth = getHeight(root.left);
        if (lefth == -1) return -1;
        int righth = getHeight(root.right);
        if (righth == -1) return -1;
        if (Math.abs(lefth - righth) > 1) {
            return -1;
        }
        return Math.max(lefth, righth) + 1;
    }
}
